package com.enterprise.projectManager.resource;

import com.enterprise.projectManager.model.Log;
import com.enterprise.projectManager.model.Project;
import com.enterprise.projectManager.model.Response;
import com.enterprise.projectManager.service.implemention.LogImpl;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequiredArgsConstructor
@RequestMapping("/crebas/log")
public class LogResource {
    private final LogImpl logService;

    // 创建一个新的日志
    @PostMapping("/create")
    public ResponseEntity<Response> createLog(@RequestBody Log log) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("newLog",logService.create(log)))
                        .message("新建日志")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build()
        );
    }

    // 删除一个日志
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Response> deleteLog(@PathVariable String id) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("deleteResult",logService.delete(id)))
                        .message("删除日志")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 更新一个项目
    @PostMapping("/update")
    public ResponseEntity<Response> updateLog(@RequestBody Log log) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("updatedLog",logService.update(log)))
                        .message("更新日志")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 查询一个项目的所有日志
    @GetMapping("/searchByProjectId/{projectId}")
    public ResponseEntity<Response> searchByProjectId(@PathVariable String projectId) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("logs",logService.searchByProjectId(projectId)))
                        .message("查询项目"+projectId+"的所有日志")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @GetMapping("/getAll")
    public ResponseEntity<Response> getAll() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("logs",logService.getAll()))
                        .message("查询所有日志")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
}
